package sysuser

import (
	"context"
	"jinrigaoping/api/platform/internal/svc"
	"jinrigaoping/api/platform/internal/types"
	"jinrigaoping/rpc/sys/sys"

	"github.com/zeromicro/go-zero/core/logx"
)

type SysUserPageLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewSysUserPageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SysUserPageLogic {
	return &SysUserPageLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SysUserPageLogic) SysUserPage(req *types.SysUserPageReq) (resp *types.SysUserPageRes, err error) {

	pageResp, err := l.svcCtx.SysRpc.SysUserPage(l.ctx, &sys.SysUserPageReq{
		Page:    req.Page,
		Limit:   req.Limit,
		Account: req.Account,
	})

	if err != nil {
		logx.Errorf("l.svcCtx.SysRpc.SysUserPage req: %v  error: %v", req, err)
		return nil, err
	}

	var list = make([]*types.SysUserPageItem, len(pageResp.List))
	for i, item := range pageResp.List {
		list[i] = &types.SysUserPageItem{
			Id:         item.Id,
			Account:    item.Account,
			Status:     item.Status,
			CreateTime: item.CreateTime,
			Nickname:   item.Nickname,
			Phone:      item.Phone,
			Email:      item.Email,
			Brief:      item.Brief,
		}
	}

	return &types.SysUserPageRes{
		Total: pageResp.Total,
		List:  list,
	}, nil
}
